//
//  HeightViewController.m
//  babyMemoryPro
//
//  Created by student on 15-7-6.
//  Copyright (c) 2015年 niit. All rights reserved.
//

#import "HeightViewController.h"
#import "AddViewController.h"
#import "UUChart.h"
#import "HeightandWeight.h"
#define WinWidth [UIScreen mainScreen].bounds.size.width
#define WinHeight [UIScreen mainScreen].bounds.size.height

#define W(x) WinWidth*x/320.0
#define H(y) WinHeight*y/568.0

@interface HeightViewController ()<UUChartDataSource,AddViewControllerDelegate>
{
    NSIndexPath *path;
    UUChart *chartView;
    int myseg;
    NSString *path1;
    NSMutableArray *heightArr;
    NSMutableArray *weightArr;
    
}

@end

@implementation HeightViewController
int i=0;


-(NSMutableArray *)heightList{
    if (!_heightList) {
        _heightList=[[NSMutableArray alloc]init];
    }
    return _heightList;
}
-(void)saveData{
    NSMutableData *data=[NSMutableData data];
    
    NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
    [archiver encodeObject:self.heightList forKey:@"myHeight"];
    [archiver finishEncoding];
    [data writeToFile:path1 atomically:YES];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    heightArr=[@[@"50"] mutableCopy];
    weightArr=[@[@"3"] mutableCopy];
    [self customNaviItem];
    path1=[NSHomeDirectory() stringByAppendingString:@"/Documents/height.plist"];
    NSData *data=[NSData dataWithContentsOfFile:path1];
    if (data.length>0) {
        NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:data];
        NSMutableArray *arr=[unarchiver decodeObjectForKey:@"myHeight"];
        [unarchiver finishDecoding];
        self.heightList=[arr mutableCopy];
        
    }
}
-(void)viewDidAppear:(BOOL)animated{

}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];


    if (heightArr==nil) {
        heightArr=[@[@"50"] mutableCopy];
        weightArr=[@[@"3"] mutableCopy];
    }
    
    else{
            for (int i=0; i<self.heightList.count; i++)     {
                HeightandWeight *model=self.heightList[i];
                [heightArr addObject:model.height];
                [weightArr addObject:model.weight];
    }
    

    }
 

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)customNaviItem{
    UISegmentedControl *seg=[[UISegmentedControl alloc]initWithItems:@[@"身高",@"体重"]];
    seg.selectedSegmentIndex=0;
    if (seg.selectedSegmentIndex == 0) {
        myseg=0;
        UIScrollView *myScrollView=[[UIScrollView alloc]init];
        
        myScrollView.frame=CGRectMake(0, 30,W(320), 500);
        [self.view addSubview:myScrollView];
        
        if (chartView) {
            [chartView removeFromSuperview];
            chartView = nil;
        }
        chartView = [[UUChart alloc]initwithUUChartDataFrame:CGRectMake(0, 0, 1000, 450)
                                                  withSource:self
                                                   withStyle:nil];
        [chartView showInView:myScrollView];
        myScrollView.contentSize=CGSizeMake(1000, 500);
    }
    
    [seg addTarget:self action:@selector(segChanged:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView=seg;

    
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
    [btn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem=rightItem;

}

-(void)add{
    [heightArr removeAllObjects];
    [weightArr removeAllObjects];
    AddViewController *addVC=[[AddViewController alloc]init];
    [self.navigationController pushViewController:addVC animated:YES];
    addVC.delegate=self;
}

-(void)segChanged:(UISegmentedControl *)seg{
    
    if (seg.selectedSegmentIndex == 0) {
        myseg=0;
        UIScrollView *myScrollView=[[UIScrollView alloc]init];

        myScrollView.frame=CGRectMake(0, 30,W(320), 500);
        [self.view addSubview:myScrollView];

        if (chartView) {
            [chartView removeFromSuperview];
            chartView = nil;
        }
        chartView = [[UUChart alloc]initwithUUChartDataFrame:CGRectMake(0, 0, 1000, 450)
                                                  withSource:self
                                                   withStyle:nil];
        [chartView showInView:myScrollView];
        myScrollView.contentSize=CGSizeMake(1000, 500);
    }
    else{
        
        myseg=1;
        UIScrollView *myScrollView=[[UIScrollView alloc]init];
        myScrollView.backgroundColor=[UIColor grayColor];
        myScrollView.frame=CGRectMake(0, 30,W(320), 500);
        [self.view addSubview:myScrollView];
//        [super viewDidLoad];
        if (chartView) {
            [chartView removeFromSuperview];
            chartView = nil;
        }
        chartView = [[UUChart alloc]initwithUUChartDataFrame:CGRectMake(0, 0, 1000, 450)
                                                  withSource:self
                                                   withStyle:nil];
        [chartView showInView:myScrollView];
        myScrollView.contentSize=CGSizeMake(1000, 500);
    }
}


- (NSArray *)getXTitles:(int)num
{
    NSMutableArray *xTitles = [NSMutableArray array];

    for (int i=0; i<num; i++) {
        NSString * str = [NSString stringWithFormat:@"%d个月",i];
        [xTitles addObject:str];
    }
    return xTitles;
}
//横坐标标题数组
- (NSArray *)UUChart_xLableArray:(UUChart *)chart
{
    return [self getXTitles:36];
}
//数值多重数组
- (NSArray *)UUChart_yValueArray:(UUChart *)chart
{   if (myseg == 0) {
    NSArray *ary = @[@"50.4",@"54.8",@"58.7",@"62",@"64.6",@"66.7",@"68.4",@"69.8",@"71.2",@"72.6",@"74",@"75.3",@"76.5",@"77.6",@"78.7",@"79.8",@"80.8",@"81.7",@"82.7",@"83.6",@"84.6",@"85.6",@"86.6",@"87.6",@"88.5",@"89.4",@"90.2",@"91.1",@"91.8",@"92.5",@"93.3",@"94",@"94.7",@"95.4",@"96.1"];
  
    
    
    return @[ary,heightArr];
}
else{
    NSArray *ary=@[@"3.72",@"4.51",@"5.68",@"6.7",@"7.45",@"8",@"8.41",@"8.76",@"9.05",@"9.33",@"9.58",@"9.83",@"10.05",@"10.26",@"10.47",@"10.68",@"10.89",@"11.09",@"11.29",@"11.50",@"11.72",@"11.93",@"12.14",@"12.34",@"12.54",@"12.73",@"12.92",@"13.11",@"13.28",@"13.46",@"13.64",@"13.81",@"13.98",@"14.15",@"14.33"];
    
    
    return @[ary,weightArr];
}
    
   
    
}

#pragma mark - @optional
//颜色数组
- (NSArray *)UUChart_ColorArray:(UUChart *)chart
{
    return @[UUGreen,UURed,UUBrown];
}
//显示数值范围
- (CGRange)UUChartChooseRangeInLineChart:(UUChart *)chart
{
    if (myseg==0) {
        return CGRangeMake(110, 10);
    }
    else{
        return CGRangeMake(20, 0);
    }
    
    
}

#pragma mark 折线图专享功能

//标记数值区域
- (CGRange)UUChartMarkRangeInLineChart:(UUChart *)chart
{
    
    return CGRangeMake(25, 75);
    
}

//判断显示横线条
- (BOOL)UUChart:(UUChart *)chart ShowHorizonLineAtIndex:(NSInteger)index
{
    return YES;
}

//判断显示最大最小值
- (BOOL)UUChart:(UUChart *)chart ShowMaxMinAtIndex:(NSInteger)index
{
    return path.row==0;
}

-(void)passValue:(HeightandWeight *)model{
    [self.heightList addObject:model];
    [self saveData];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
